//
//  PSChatSentenceView.m
//  Btpin
//
//  Created by Vic on 2020/3/29.
//  Copyright © 2020 Vic. All rights reserved.
//

#import "PSChatSentenceView.h"

@interface PSChatSentenceView ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *cTableView;
}
@end

@implementation PSChatSentenceView

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)initWithFrame:(CGRect)frame target:(id)target {
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
        self.delegate = target;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chatMessageOK) name:NotificationFor_ChatMessage_OK object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chatMessageDeleteOK) name:NotificationFor_ChatMessage_Delete_OK object:nil];
    }
    return self;
}
- (void)initUI {
    self.backgroundColor = COLOR_OF_WHITE;
    cTableView = [UIToolsControl initTableViewWithFrame:CGRectMake(0, 0, WIDTH, 180) target:self style:UITableViewStylePlain];
    cTableView.rowHeight = 60;
    cTableView.tableFooterView = [[UIView alloc] init];
    cTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self addSubview:cTableView];
    
    UIView *addView = [UIToolsControl initViewWithFrame:CGRectMake(0, EndY(cTableView), WIDTH*0.5, 50) backgroundColor:COLOR_OF_WHITE];
    [addView borderColor:THEMECOLOR_OF_Line_Color borderWidth:lineH borderType:(BorderSideTypeTop)];
    [self addSubview:addView];
    UIButton *addBtn = [UIToolsControl initButtonWithFrame:CGRectMake((WIDTH*0.5-90)*0.5, 5, 90, 40) imageName:nil target:self action:@selector(addBtnTap) title:@"添加" titleColor:COLOR_OF_WHITE];
    addBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    addBtn.layer.cornerRadius = 4;
    addBtn.layer.masksToBounds = YES;
    addBtn.titleLabel.font = Font(Font_15);
    [addView addSubview:addBtn];
    
    UIView *manegeView = [UIToolsControl initViewWithFrame:CGRectMake(EndX(addView), StartY(addView), ViewWidth(addView), ViewHeight(addView)) backgroundColor:COLOR_OF_WHITE];
    [manegeView borderColor:THEMECOLOR_OF_Line_Color borderWidth:lineH borderType:(BorderSideTypeLeft | BorderSideTypeTop)];
    [self addSubview:manegeView];
    UIButton *manegeBtn = [UIToolsControl initButtonWithFrame:CGRectMake((WIDTH*0.5-90)*0.5, 5, 90, 40) imageName:nil target:self action:@selector(manegeBtnTap) title:@"管理" titleColor:COLOR_OF_WHITE];
    manegeBtn.backgroundColor = THEMECOLOR_OF_ORANGE;
    manegeBtn.layer.cornerRadius = 4;
    manegeBtn.layer.masksToBounds = YES;
    manegeBtn.titleLabel.font = Font(Font_15);
    [manegeView addSubview:manegeBtn];
}
- (void)chatMessageDeleteOK {
    [cTableView reloadData];
}
- (void)chatMessageOK {
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    
    [BaseNetManager requestWithMethod:POST path:Chat_GetSentence_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            if ([data isKindOfClass:[NSArray class]]) {
                if ([DataToolsControl sharedManager].chatSentenceArray && [DataToolsControl sharedManager].chatSentenceArray.count) {
                    [[DataToolsControl sharedManager].chatSentenceArray removeAllObjects];
                }
                for (NSDictionary *dic in data) {
                    ChatSentenceModel *model = [[ChatSentenceModel alloc] init];
                    [model setValuesForKeysWithDictionary:dic];
                    [[DataToolsControl sharedManager].chatSentenceArray addObject:model];
                }
                [self->cTableView reloadData];
            }
        }
    } failurBlock:^(NSError * _Nonnull error) {
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [DataToolsControl sharedManager].chatSentenceArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    ChatMessageManageCell *cell = [tableView dequeueReusableCellWithIdentifier:isCellID];
//    if (!cell) {
//        cell = [[ChatMessageManageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:isCellID];
//        cell.delegate = self;
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    }
//
////    ChatSentenceModel *model = dataArray[indexPath.row];
//    ChatSentenceModel *model = [DataToolsControl sharedManager].chatSentenceArray[indexPath.row];
//    [cell reloadDataForModel:model indexPath:indexPath];
//
//    return cell;
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:isCellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:isCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = Font(Font_15);
        cell.textLabel.textColor = THEMECOLOR_OF_TEXT_333333;
        cell.textLabel.numberOfLines = 0;
    }
    
    ChatSentenceModel *model = [DataToolsControl sharedManager].chatSentenceArray[indexPath.row];
    cell.textLabel.text = model.text;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChatSentenceModel *model = [DataToolsControl sharedManager].chatSentenceArray[indexPath.row];
    CGFloat height = [UIToolsControl textHeight:model.text size:CGSizeMake(WIDTH-50, MAXFLOAT) font:Font_15]+35;
    return height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatSentenceViewSelectIndex:)]) {
        ChatSentenceModel *model = [DataToolsControl sharedManager].chatSentenceArray[indexPath.row];
        [self.delegate chatSentenceViewSelectIndex:model];
    }
}
- (void)addBtnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatSentenceViewAdd)]) {
        [self.delegate chatSentenceViewAdd];
    }
}
- (void)manegeBtnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatSentenceViewManage)]) {
        [self.delegate chatSentenceViewManage];
    }
}

@end
